package com.home.andromo;

import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.provider.BaseColumns;
import android.util.Log;

public class AndromoDbHelper extends SQLiteOpenHelper {

	static final String TAG = "DATABASEHelper";
	static final String DB_NAME = "andromodb.db"; //
	static final int DB_VERSION = 1; //
	static final String TABLE = "appscategorias"; //
	static final String C_ID = BaseColumns._ID;
	static final String C_PACKAGE_NAME = "package_name";
	static final String C_NAME = "app_name";
	static final String C_CATEGORY = "app_category";
	static final String[] DB_TEXT_COLUMNS = { C_PACKAGE_NAME };

	Context context;
	private SQLiteDatabase db; // REPRESENTACION DE LA BASE DE DATOS

	public AndromoDbHelper(Context context) {
		super(context, DB_NAME, null, DB_VERSION);
		this.context = context;
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		String sql = "create table if not exists " + TABLE + " (" + C_ID
				+ " INTEGER PRIMARY KEY AUTOINCREMENT, " + C_PACKAGE_NAME
				+ " text, " + C_NAME + " text, " + C_CATEGORY + " int)"; //
		db.execSQL(sql);
		Log.d(TAG, "onCreated sql: " + sql);
	}

	// Called whenever newVersion != oldVersion
	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { //
		db.execSQL("drop table if exists " + TABLE); // drops the old
		Log.d(TAG, "onUpdated");
		onCreate(db); // run onCreate to get new database
	}

	// REGISTRO DE ARCHIVOS

	public void guardarApps(String paquete, String nombre, int categoria) {
		try {
			// Open the database for writing
			db = getWritableDatabase();
			ContentValues values = new ContentValues();
			values.clear();
			values.put(C_PACKAGE_NAME, paquete);
			values.put(C_NAME, nombre);
			values.put(C_CATEGORY, categoria);
			try {
				db.insertOrThrow(TABLE, null, values); //
				Log.d("guarde: " + TAG,
						String.format("%s: %s %s", nombre, paquete, categoria));
			} catch (SQLException e) { //
				// Ignore exception
			}
			// Close the database
			db.close(); //
			Log.d(TAG, "Updater ran");
			Thread.sleep(100);
		} catch (InterruptedException e) {

		}
	}
	
	// ELIMINACION
	
	public void quitarApp(String paquete, String nombre, int categoria) {
		try {
			// Open the database for writing
			db = getWritableDatabase();			
			try {				
				ContentValues values = new ContentValues();
				values.clear();
				values.put(C_PACKAGE_NAME, paquete);
				values.put(C_NAME, nombre);
				values.put(C_CATEGORY, categoria);				
				db.update(TABLE, values, C_PACKAGE_NAME + "= '"+ paquete +"'" , null);			
			} finally {
				db.close();
			}
			Log.d(TAG, "Updater ran");
			Thread.sleep(100);
		} catch (InterruptedException e) {

		}
	}


	// CONSULTAS A LA BASE DE DATOS

	public ArrayList<String> SelectDatosPorIdCategoria(int cat) {
		
		SQLiteDatabase db = getReadableDatabase();
		ArrayList<String> retorno = new ArrayList<String>();

		try {
			Cursor cursor = db.query(TABLE, DB_TEXT_COLUMNS, C_CATEGORY + "='"
					+ cat + "'", null, null, null, null);
			try {
				while (cursor.moveToNext()) {
					retorno.add(cursor.getString(0));
				}
				for(int i = 0 ; i < retorno.size() ; i++){
					Log.v("ARRAYLISTA  ", retorno.get(i));
				}
				return retorno;
			} finally {
				cursor.close();
			}
		} finally {
			db.close();
		}
	}
}